package view

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.util.TypedValue
import android.view.Gravity
import android.view.View
import android.widget.FrameLayout
import android.widget.LinearLayout
import android.widget.TextView


class StepView : FrameLayout, ValueAnimator.AnimatorUpdateListener {
    constructor(context: Context) : super(context)

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)

    private val mLinearLayout: LinearLayout
    private val mTextTitle: TextView
    private val mTextTak: TextView
    private val mTextPreHeat: TextView
    private val mTextHeat: TextView
    private val mTextDeliver: TextView
    private var animator: ValueAnimator? = null
    private var target: TextView? = null

    init {
        mLinearLayout = LinearLayout(context)
        mTextTitle = TextView(context)
        mTextTak = TextView(context)
        mTextPreHeat = TextView(context)
        mTextHeat = TextView(context)
        mTextDeliver = TextView(context)

        /**
         * @brief 设置Linear, Title
         */
        val params = FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.MATCH_PARENT,
            FrameLayout.LayoutParams.MATCH_PARENT
        )
        mLinearLayout.layoutParams = params
        mTextTitle.layoutParams = params
        addView(mLinearLayout)
        addView(mTextTitle)
        mTextTitle.gravity = Gravity.CENTER
        mTextTitle.setTextColor(resources.getColor(android.R.color.holo_red_light))
        mTextTitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30f)

        mLinearLayout.orientation = LinearLayout.HORIZONTAL
        val params2 = LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT)
        params2.weight = 1f

        mTextTak.layoutParams = params2
        mTextTak.gravity = Gravity.CENTER
        mTextTak.setTextColor(Color.GRAY)
        mTextTak.text = "Taking"
        mTextTak.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20f)

        mTextPreHeat.layoutParams = params2
        mTextPreHeat.gravity = Gravity.CENTER
        mTextPreHeat.setTextColor(Color.GRAY)
        mTextPreHeat.text = "Preheating"
        mTextPreHeat.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20f)

        mTextHeat.layoutParams = params2
        mTextHeat.gravity = Gravity.CENTER
        mTextHeat.setTextColor(Color.GRAY)
        mTextHeat.text = "Heating"
        mTextHeat.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20f)

        mTextDeliver.layoutParams = params2
        mTextDeliver.gravity = Gravity.CENTER
        mTextDeliver.setTextColor(Color.GRAY)
        mTextDeliver.text = "Delivering"
        mTextDeliver.setTextSize(TypedValue.COMPLEX_UNIT_PX, 20f)

        mLinearLayout.addView(mTextTak)
        mLinearLayout.addView(mTextPreHeat)
        mLinearLayout.addView(mTextHeat)
        mLinearLayout.addView(mTextDeliver)
    }

    override fun onAnimationUpdate(animation: ValueAnimator) {
        if (target != null) {
            val v = animation.animatedValue as Int
            target!!.setTextColor(v)
        }
    }

    private fun stopAnimator() {
        if (animator != null) {
            animator!!.removeAllUpdateListeners()
            animator!!.cancel()
            animator = null
            target = null
        }
    }

    private fun startAnimator(view: TextView) {
        if (animator != null) {
            animator!!.removeAllUpdateListeners()
            animator!!.cancel()
            animator = null
        }
        animator = ValueAnimator.ofInt(0xFF000000.toInt(), 0xFFFFFFFF.toInt())
        animator!!.addUpdateListener(this)
        animator!!.duration = 120 * 1000
        target = view
        animator!!.repeatCount = ObjectAnimator.INFINITE
        animator!!.repeatMode = ObjectAnimator.REVERSE
        animator!!.start()
    }

    fun setTitle(title: String) {
        mTextTitle.visibility = View.VISIBLE
        mLinearLayout.visibility = View.GONE
        stopAnimator()
        mTextTitle.text = title
    }

    fun showWith2(step: Int = 0) {
        mTextTitle.visibility = View.GONE
        mLinearLayout.visibility = View.VISIBLE
        mTextTak.visibility = View.VISIBLE
        mTextDeliver.visibility = View.VISIBLE
        mTextPreHeat.visibility = View.GONE
        mTextHeat.visibility = View.GONE

        when (step) {
            0 -> {
                stopAnimator()
                mTextDeliver.setTextColor(Color.GRAY)
                startAnimator(mTextTak)
            }

            1 -> {
                stopAnimator()
                mTextTak.setTextColor(Color.GRAY)
                startAnimator(mTextDeliver)
            }
        }
    }

    fun showWith3(step: Int = 0) {
        mTextTitle.visibility = View.GONE
        mLinearLayout.visibility = View.VISIBLE
        mTextTak.visibility = View.VISIBLE
        mTextDeliver.visibility = View.VISIBLE
        mTextPreHeat.visibility = View.GONE
        mTextHeat.visibility = View.VISIBLE

        when (step) {
            0 -> {
                stopAnimator()
                startAnimator(mTextTak)
                mTextHeat.setTextColor(Color.GRAY)
                mTextDeliver.setTextColor(Color.GRAY)
            }

            1 -> {
                stopAnimator()
                mTextTak.setTextColor(Color.GRAY)
                startAnimator(mTextHeat)
                mTextDeliver.setTextColor(Color.GRAY)
            }

            2 -> {
                mTextTak.setTextColor(Color.GRAY)
                mTextHeat.setTextColor(Color.GRAY)
                startAnimator(mTextDeliver)
            }
        }
    }

    fun showWith4(step: Int = 0) {
        mTextTitle.visibility = View.GONE
        mLinearLayout.visibility = View.VISIBLE
        mTextTak.visibility = View.VISIBLE
        mTextDeliver.visibility = View.VISIBLE
        mTextPreHeat.visibility = View.VISIBLE
        mTextHeat.visibility = View.VISIBLE

        when (step) {
            0 -> {
                stopAnimator()
                startAnimator(mTextTak)
                mTextPreHeat.setTextColor(Color.GRAY)
                mTextHeat.setTextColor(Color.GRAY)
                mTextDeliver.setTextColor(Color.GRAY)
            }

            1 -> {
                stopAnimator()
                mTextTak.setTextColor(Color.GRAY)
                startAnimator(mTextPreHeat)
                mTextHeat.setTextColor(Color.GRAY)
                mTextDeliver.setTextColor(Color.GRAY)
            }

            2 -> {
                stopAnimator()
                mTextHeat.setTextColor(Color.GRAY)
                mTextPreHeat.setTextColor(Color.GRAY)
                startAnimator(mTextHeat)
                mTextDeliver.setTextColor(Color.GRAY)
            }

            3 -> {
                stopAnimator()
                mTextTak.setTextColor(Color.GRAY)
                mTextPreHeat.setTextColor(Color.GRAY)
                mTextHeat.setTextColor(Color.GRAY)
                startAnimator(mTextDeliver)
            }
        }
    }
}
